#include "inc_common.fxh"

float4x4 Rotation;

#define LAYERS 4

// Layers
DECLARE_TEXTURE(0);
DECLARE_TEXTURE(1);
DECLARE_TEXTURE(2);
DECLARE_TEXTURE(3);

// Layers Data
float2 Offset[LAYERS];
float2 Scale[LAYERS];
float4 Color[LAYERS];

// AlphaMap
float2 TerrainOffset;
DECLARE_TEXTURE(LAYERS);

// Cursor
DECLARE_TEXTURE(15);
float2 CursorPosition;
float2 CursorScale;
float4 CursorColor;
bool ShowCursor;

S_PP2NT VertexShaderFunction(S_PNT_I input)
{
    S_PP2NT output;

	output.Position = CalcPos(input.Position);
	output.Normal = normalize(mul(input.Normal, Rotation));
	output.TexCoords = input.TexCoords;
	output.Position2D = 1;
	output.Position2D.xz = (input.Position.xz + TerrainOffset) / (TerrainOffset * 2);

    return output;
}

float4 PixelShaderFunction(S_PP2NT input) : COLOR0
{
	float4 color = 0;

	float4 blend = SAMPLE_TEXTURE(LAYERS, input.Position2D.xz);

	color += SAMPLE_TEXTURE(0, (input.TexCoords + Offset[0]) / Scale[0]) * Color[0] * blend.r;
	color += SAMPLE_TEXTURE(1, (input.TexCoords + Offset[1]) / Scale[1]) * Color[1] * blend.g;
	color += SAMPLE_TEXTURE(2, (input.TexCoords + Offset[2]) / Scale[2]) * Color[2] * blend.b;
	color += SAMPLE_TEXTURE(3, (input.TexCoords + Offset[3]) / Scale[3]) * Color[3] * blend.a;

	color *= float4(CalculateShadow(input.Normal), 1);

	if(ShowCursor)
		color += SAMPLE_TEXTURE(15, (input.Position2D.xz + CursorPosition) * TerrainOffset * 2 / CursorScale) * CursorColor;

    return saturate(color);
}

technique TerrainSimple
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
